// pages/tenant/tenant.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'expend',
    apply:[],
    tenant:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.getTenant();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  handleType(event) {
    const type = event.currentTarget.dataset.type;
    this.setData({
      type: type,
    });
  },
  getTenant(){
    var that = this
    app.call.getData('wxapp/userTenant', function(data){
      var tenant = []
      var apply = []
      data.data.forEach(function(e){
        if(e.status == 1){
          tenant.push(e)
        }else{
          apply.push(e)
        }
      })
      that.setData({
        tenant:tenant,
        apply:apply
      })
    },function(){});
  },
  agree(e){
    var that = this
    var member_room_id =  e.currentTarget.dataset.id
    if(member_room_id == ''){
      app.showTip('数据异常')
      return ;
    }
    var postData = {
      member_room_id:member_room_id,
    }
    app.call.request('wxapp/agree',postData, function(data){
      app.showTip('操作成功');
      that.getTenant();
    },function(){});
  },
  relieve(e){
    var that = this
    var member_room_id =  e.currentTarget.dataset.id
    if(member_room_id == ''){
      app.showTip('数据异常')
      return ;
    }
    var postData = {
      member_room_id:member_room_id,
    }
    app.call.request('wxapp/relieve',postData, function(data){
      app.showTip('解绑成功');
      that.getTenant();
    },function(){});
  },
  Unbinding(e){
    var that = this
    var member_room_id =  e.currentTarget.dataset.id
    var tenant_id =  e.currentTarget.dataset.tenant_id
    if(member_room_id == '' || tenant_id == ''){
      app.showTip('数据异常')
      return ;
    }
    var postData = {
      member_room_id:member_room_id,
      tenant_id:tenant_id
    }
    wx.showModal({
      title: '温馨提示',
      content: '是否解除该名租客和房间的绑定关系',
      showCancel: true,
      cancelText: '取消',
      confirmText: '确定',
      success: (result) => {
        if (result.confirm) {
          app.call.request('wxapp/delTenant',postData, function(data){
            app.showTip('解除成功');
            that.getTenant();
          },function(){});
        }
      },
      fail: () => {},
      complete: () => {}
    })
  }
})